global root_dir = "`1'"
include "$root_dir/code/config/config.do"

cap noi log using ${log_dir}/Table_A28_macro_descriptives.log, replace name(tabl)

capture noi {  

    *Table: Low-skill wages and the skill-premium in manufacturing sector for selected countries
    ************************************************************************************************

    texdoc init ${tab_dir}/appendix/Table_A28_macro_descriptives.tex, replace
    texdoc stlog, nolog

    clear
    *load manufacturing wages data
    use ${final_dir}/wage_combined_final_allcodes.dta, clear
    keep if code=="TOT" | code=="D"

    *restrict to table countries
    local countries "ind mex bgr usa bel swe fin"

    keep if code == "D"
    keep year country ls_wage_pi_USD_95 hs_wage_pi_USD_95
    keep if year == 1995 | year ==2009
    keep if inlist(country, "ind", "mex", "bgr", "usa", "bel", "swe", "fin")
    kountry country, from(iso3c)
    drop country
    ren NAMES_STD country
    ren  ls_wage_pi_USD_95 LSW_
    ren hs_wage_pi_USD_95 HSW_
    gen SP_ = HSW / LSW 
    reshape wide LSW HSW SP, i(country) j(year)
    gsort +LSW_2009

    * map the data into formatted texdoc locals
    forval i = 1/`=_N' { 
            texdoc local C`i' country[`i']
            if (`C`i'' == "United_States") texdoc local C`i' "United States"
            foreach vr in LSW HSW SP { 
                foreach yr in 1995 2009 {
                    sleep 1000
                    texdoc local C`i'_`vr'_`yr' : display %10.2f `vr'_`yr'[`i']
                }
            }
    }

    * output table (via texdoc)
    texdoc local notes "Wages data, taken from WIOD. The table shows manufacturing low-skill and high-skill wages (technically labor costs) deflated by (manufacturing) PPI and converted to USD using average 1995 exchange rates. Skill-premium is the ratio of high-skill to low-skill wages. The table shows the three countries with the lowest low-skill wages in 2009, the three with the highest and the US."
    sleep 500
    texdoc stlog close
    qui include ${code_dir}/config/texdoctools.do
    tex `texpreamble'

    /*tex
    \begin{tabular}{@{}l*{6}{S[table-format = 2.2]}@{}}
    \toprule
    Country & \multicolumn{2}{c}{\makecell{Low-skill wages \\ (1995\$)}} & \multicolumn{2}{c}{\makecell{High-skill wages \\ (1995\$)}} & \multicolumn{2}{c}{\makecell{Skill-premium \\ (HSW/LSW)}} \\
    \cmidrule(lr){2-3}\cmidrule(lr){4-5}\cmidrule(lr){6-7} & \mc{1995} & \mc{2009} & \mc{1995} & \mc{2009} & \mc{1995} & \mc{2009} \\
    \midrule
    India & `C1_LSW_1995' & `C1_LSW_2009' & `C1_HSW_1995' & `C1_HSW_2009' & `C1_SP_1995' & `C1_SP_2009' \\
    Mexico & `C2_LSW_1995' & `C2_LSW_2009' & `C2_HSW_1995' & `C2_HSW_2009' & `C2_SP_1995' & `C2_SP_2009' \\
    Bulgaria & `C3_LSW_1995' & `C3_LSW_2009' & `C3_HSW_1995' & `C3_HSW_2009' & `C3_SP_1995' & `C3_SP_2009' \\
    United States & `C4_LSW_1995' & `C4_LSW_2009' & `C4_HSW_1995' & `C4_HSW_2009' & `C4_SP_1995' & `C4_SP_2009' \\
    Belgium & `C5_LSW_1995' & `C5_LSW_2009' & `C5_HSW_1995' & `C5_HSW_2009' & `C5_SP_1995' & `C5_SP_2009' \\
    Sweden & `C6_LSW_1995' & `C6_LSW_2009' & `C6_HSW_1995' & `C6_HSW_2009' & `C6_SP_1995' & `C6_SP_2009' \\
    Finland & `C7_LSW_1995' & `C7_LSW_2009' & `C7_HSW_1995' & `C7_HSW_2009' & `C7_SP_1995' & `C7_SP_2009' \\
    \bottomrule
    \end{tabular}
    tex*/
    tex `texpostamble'
    texdoc close

}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}